import time
from jsonpath import jsonpath
from locust import HttpUser, task, constant


class QuickStartUser(HttpUser):
    host = "http://127.0.0.1:8000"
    wait_time = constant(1)

    @task
    def api(self):
        print(time.strftime("%Y-%m-%d-%H-%M-%S"))
        with self.client.get(url="/locust_test", catch_response=True, name='测试') as response:
            print(response.elapsed.total_seconds())
            print(response.json())
            if response.status_code == 200:
                try:
                    assert jsonpath(response.json(), '$..code')[0] == 1
                except:
                    response.failure("Timeout")
            else:
                response.failure(response.status_code)
